﻿@model Telerik.Web.Mvc.GridModel<LanguageResourceModel>
@using Telerik.Web.Mvc.UI;
@using System.Linq;
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-content.png")" alt="" />
        @T("Admin.Configuration.Languages.Localization")
    </div>
</div>
@if (ViewBag.AllLanguages != null)
{
    <table class="adminContent" style="margin-bottom:10px;">
        <tr>
            <td class="adminTitle">
                @T("Admin.Configuration.Languages.Select")
            </td>
            <td class="adminData">
                @(Html.Telerik().ComboBox().Name("select-language")
                .ClientEvents(x => x.OnChange("onLanguageSelected"))
                .BindTo(ViewBag.AllLanguages))
                <script type="text/javascript">
                    function onLanguageSelected(e) {
                        window.location = "/Admin/Language/Resources?languageId=" + e.value;
                    }
                </script>
            </td>
        </tr>
    </table>
}
<table class="adminContent">
    <tr>
        <td>
            @(Html.Telerik().Grid(Model.Data)
                    .Name("resources-grid")
                    .DataKeys(x =>
                    {
                        x.Add(y => y.Id).RouteKey("Id");
                        x.Add(y => y.LanguageId).RouteKey("languageId");
                    })
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.LanguageName)
                            .Width(100)
                            .ReadOnly()
                            .Filterable(false);
                        columns.Bound(x => x.Name).EditorTemplateName("String").Width(300);
                        columns.Bound(x => x.Value).EditorTemplateName("String");
                        columns.Command(commands =>
                        {
                            commands.Edit();
                            commands.Delete();
                        }).Width(180);

                    })
                    .ToolBar(x => x.Insert())
                    .Editable(x =>
                    {
                        x.Mode(GridEditMode.InLine);
                    })
                    .Filterable()
                    .Pageable(settings => settings.PageSize(gridPageSize).Total(Model.Total).Position(GridPagerPosition.Both))
                    .DataBinding(dataBinding =>
                    {
                        dataBinding.Ajax().Select("Resources", "Language")
                            .Update("ResourceUpdate", "Language")
                            .Delete("ResourceDelete", "Language")
                            .Insert("ResourceAdd", "Language", new { id = ViewBag.LanguageId });
                    })
                    .EnableCustomBinding(true)
                    .ClientEvents(x => x.OnEdit("onResourceEdit"))
                    .ClientEvents(x => x.OnError("Grid_onError")))
        </td>
    </tr>
</table>
<script type="text/javascript">
    function onResourceEdit(e) {
        if (e.mode == "insert") {
            $(e.form).find("td:first").html("@ViewBag.LanguageName");
        }
    }
    function Grid_onError(e) {
        alert(e.XMLHttpRequest.responseText);
        e.preventDefault();
    }
</script>
